package com.wlw.eye.adapter

import android.content.Intent
import android.os.Parcelable
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.wlw.eye.R
import com.wlw.eye.mvp.model.bean.HotBean
import com.wlw.eye.mvp.model.bean.VideoBean
import com.wlw.eye.ui.VideoDetailActivity
import com.wlw.eye.utils.ImageLoadUtils
import com.wlw.eye.utils.ObjectSaveUtils
import com.wlw.eye.utils.SPUtils
import kotlinx.android.synthetic.main.item_feed_result.view.*
import org.jetbrains.anko.onClick
import java.text.SimpleDateFormat

class FeedAdapter(list: ArrayList<HotBean.ItemListBean.DataBean>) : RecyclerView.Adapter<BaseHolder>() {
    var list: ArrayList<HotBean.ItemListBean.DataBean>? = null

    init {
        this.list = list
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseHolder = BaseHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_feed_result, parent, false))

    override fun getItemCount(): Int = list?.size ?: 0

    override fun onBindViewHolder(holder: BaseHolder, position: Int) {
        bindView(holder.itemView, list?.get(position)!!)
    }

    private fun bindView(itemView: View, bean: HotBean.ItemListBean.DataBean) {
        val photoUrl: String? = bean.cover?.feed
        photoUrl?.let { ImageLoadUtils.display(itemView.context, itemView.iv_photo, it) }
        val title = bean.title
        itemView.tv_title?.text = title
        val category = bean.category
        val duration = bean.duration
        val minute = duration.div(60)
        val second = duration.minus(minute.times(60))
        val releaseTime = bean.releaseTime

        val smf = SimpleDateFormat("MM-dd")
        var date = smf.format(releaseTime)
        val realMinute = if (minute < 10) "0$minute" else minute.toString()
        val realSecond = if (second < 10) "0$second" else second.toString()
        itemView.tv_detail.text = bean.description
        itemView.onClick {
            val intent = Intent(itemView.context, VideoDetailActivity::class.java)
            val desc = bean.description
            val playUrl = bean.playUrl
            val blurred = bean.cover?.blurred
            val collect = bean.consumption?.collectionCount
            val share = bean.consumption?.shareCount
            val reply = bean.consumption?.replyCount
            val time = System.currentTimeMillis()
            val videoBean = VideoBean(photoUrl, title, desc, duration, playUrl, category
                    , blurred, collect, share, reply, time)
            var url = SPUtils.getInstance(itemView.context, "beans").getString(playUrl!!)
            if (url.isEmpty()) {
                var count = SPUtils.getInstance(itemView.context, "beans").getInt("count",-1)
                count = if (count != -1) count.inc() else 1
                SPUtils.getInstance(itemView.context, "beans").put("count", count)
                SPUtils.getInstance(itemView.context, "beans").put(playUrl, playUrl)
                ObjectSaveUtils.saveObject(itemView.context, "bean$count", videoBean)
            }
            intent.putExtra("data", videoBean as Parcelable)
            itemView.context.startActivity(intent)
        }
    }
}